package com.wkbb.sports.websocket;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * websocket的注解@ServerEndpoint不支持注入，可以用SpringUtils获取IOC实例
 * 方法二【推荐】
 * 解决websocket下实例无法注入问题
 * @author lbh
 * 本工具类通过ApplicationContextAware实现
 *
 *  * 传入String格式的类名
 *  * 传入类.Class的类
 *  * 传入String格式的类名 & 传入类.Class的类
 *  * 本工具类通过BeanFactoryPostProcessor实现
 */
@Component
public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext context = null;
	
	public static ApplicationContext getContext()
	{
		return context;
	}
	
	public static <T>T getBean(String beanName , Class<T> obj) {  
		return (T) context.getBean(beanName, obj);
	}

	public static <T>T getBean(String beanName) {
		return (T) context.getBean(beanName);
	}

	public static <T>T getBean(Class<T> obj) {
		return (T) context.getBean(obj);
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		System.err.println("~~~~~~~~CHECKED~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		SpringBeanUtil.context = arg0;
		
	}
}
